package cn.changeforyou.web.cloud.webBase.common.model;

import cn.changeforyou.base.exception.ErrorInfo;
import cn.changeforyou.base.exception.JsonException;
import cn.changeforyou.base.model.ResultInfo;

public class Result<T> implements ResultInfo {
    private int code;
    private String message;
    private T data;

    public Result(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public Result(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static Result success(){
        return new Result(ResultInfo.SUCCESS_CODE, ResultInfo.SUCCESS_MESSAGE);
    }

    public static <T>Result<T> success(T data){
        return new Result<T>(ResultInfo.SUCCESS_CODE, ResultInfo.SUCCESS_MESSAGE, data);
    }

    public static Result fail(ErrorInfo info){
      return new Result(info.unitCode(), info.getMessage());
    }

    public static Result fail(JsonException exception){
        return new Result(exception.getCode(), exception.getMessage());
    }


}
